<?php

namespace App\Http\Controllers\Admin;

use App\Models\Orm\AwAuditRecord;
use App\Models\Orm\CooperativeCompany;
use App\Models\Orm\SmsNew;
use App\Models\Orm\Product;
use App\Models\Orm\ProductAudit;
use App\Models\Orm\ProductLabels;
use App\Models\Orm\ProjectProduct;
use App\Models\Orm\SecondsKill;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

/**
 * 产品模块控制器
 *
 * Class ProductController
 * @package App\Http\Controllers\Admin
 */
class SmsNewController extends Controller
{
    /**
     * 产品列表(展示页)
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $user = auth('admin')->user();

        $data = SmsNew::with('product')->orderBy('sort','asc')->get();
        return view('admin.smsnew.index', compact('data'));

    }

    /**
     * 新建产品审核
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $user = auth('admin')->user();

        if ($request->isMethod('post')) {

            $request->flash();
            $productIds = $request->input('product_id');
            foreach($productIds as $key=>$value)
            {
                $recommends = SmsNew::select('sort')->orderBy('sort','desc')->limit(1)->first();
                $sort = $recommends ? $recommends->sort : 0;
                $data['product_id'] = intval($value);
                $data['sort'] = intval( $sort+1);
                SmsNew::insert($data);
            }
            return redirect('/admin/smsnew/index')->with(['success' => '您的申请已提交,请耐心等等待审核']);
        }

        $data = new SmsNew();
        return view('admin.smsnew.create', compact('data'));
    }



    /**
     * 修改产品详情审核
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function delete(Request $request, $id)
    {
        SmsNew::destroy($id);
        return redirect('/admin/smsnew/index')->with(['info' => '删除成功']);
    }

    /**
     * 升序
     * @param Request $request
     * @return int
     */
    public function ajaxShengXu(Request $request)
    {
        $id = $request->input('id');
        $current_recommend = SmsNew::find($id);
        $current_recommend_sort = $current_recommend ? $current_recommend->sort : 0;

        $front_recommend = SmsNew::where('sort','<',$current_recommend_sort)->orderBy('sort','desc')->limit(1)->first();
        $front_recommend_sort = $front_recommend ? $front_recommend->sort : 0;


        if ($front_recommend_sort)
        {
            $front_recommend__new_sort = $current_recommend_sort;
            $current_recommend_new_sort = $front_recommend_sort;
            DB::transaction(function () use ($front_recommend__new_sort, $current_recommend_new_sort,$front_recommend,$current_recommend) {
                $front_recommend->sort = $front_recommend__new_sort;
                $front_recommend->save();
                $current_recommend->sort = $current_recommend_new_sort;
                $current_recommend->save();
            });
            return 1;

        }

    }


    /**
     * 降序
     * @param Request $request
     * @return int
     */
    public function ajaxjiangXu(Request $request)
    {
        $id = $request->input('id');
        $current_recommend = SmsNew::find($id);
        $current_recommend_sort = $current_recommend ? $current_recommend->sort : 0;

        $next_recommend = SmsNew::where('sort','>',$current_recommend_sort)->orderBy('sort','asc')->limit(1)->first();
        $next_recommend_sort = $next_recommend ? $next_recommend->sort : 0;


        if ($next_recommend_sort)
        {
            $next_recommend__new_sort = $current_recommend_sort;
            $current_recommend_new_sort = $next_recommend_sort;
            DB::transaction(function () use ($next_recommend__new_sort, $current_recommend_new_sort,$next_recommend,$current_recommend) {
                $next_recommend->sort = $next_recommend__new_sort;
                $next_recommend->save();
                $current_recommend->sort = $current_recommend_new_sort;
                $current_recommend->save();
            });
            return 1;

        }

    }



}
